# 数学 O(N)

class Solution:
    def maxValue(self, n: str, x: int) -> str:
        if n[0] == "-":
            for i in range(1, len(n)):
                if int(n[i]) > x:
                    return n[:i] + str(x) + n[i:]
            return n + str(x)
        else:
            for i in range(len(n)):
                if int(n[i]) < x:
                    return n[:i] + str(x) + n[i:]
            return n + str(x)


if __name__ == "__main__":
    print(Solution().maxValue(n="99", x=9))  # "999"
    print(Solution().maxValue(n="-13", x=2))  # "-123"

    # 测试用例81/97
    print(Solution().maxValue(n="-132", x=3))  # "-123"

    # 自制用例
    print(Solution().maxValue(n="99", x=3))  # "993"
    print(Solution().maxValue(n="125", x=4))  # "4125"
    print(Solution().maxValue(n="532", x=4))  # "5432"
    print(Solution().maxValue(n="-13", x=4))  # "-134"
